package admin

import (
	"context"

	"server/internal/svc"
	"server/internal/types"
	"server/models"
	"server/utils"

	"github.com/zeromicro/go-zero/core/logx"
)

type SpecValueListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSpecValueListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SpecValueListLogic {
	return &SpecValueListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SpecValueListLogic) SpecValueList(req *types.SpecValueListReq) (resp []types.SpecValueListResp, err error) {
	tx := utils.Db.Model(&models.SpecValue{}).Joins("LEFT JOIN spec_template ON spec_values.spec_template_id = spec_template.id").Joins("LEFT JOIN spec_keys ON spec_values.spec_key_id = spec_keys.id")
	if req.Value != "" {
		tx.Where("spec_values.value LIKE ?", "%"+req.Value+"%")
	}
	if req.SpecKeyID != 0 {
		tx.Where("spec_values.spec_key_id = ?", req.SpecKeyID)
	}
	tx = tx.Select("spec_values.*,spec_template.name as spec_template_name,spec_keys.name as spec_key_name").Order("spec_values.id desc")
	resp = make([]types.SpecValueListResp, 0)
	utils.TxFind(tx, &resp)
	return resp, nil
}
